package com.example.takeaway.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author 邢晨旭
 * {@code @date} 2023/10/12
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {
    private Integer code;
    private String message;
    private T data;

    public static <T> R<T> ok() {
        return new R<T>(100000, "success", null);
    }

    public static <T> R<T> ok(T data) {
        return new R<T>(100000, "success", data);
    }

    public static <T> R<T> err() {
        return new R<T>(999999, "error", null);
    }

    public static <T> R<T> err(Integer code) {
        return new R<T>(code, "error", null);
    }

    public static <T> R<T> err(String message) {
        return new R<T>(999999, message, null);
    }

    public static <T> R<T> err(Integer code, String message) {
        return new R<T>(code, message, null);
    }

    public static <T> R<T> err(ResponseEnum responseEnum) {
        return new R<T>(responseEnum.getCode(), responseEnum.getMessage(), null);
    }

    public R<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public R<T> message(String message) {
        this.message = message;
        return this;
    }

    public R<T> data(T data) {
        this.data = data;
        return this;
    }

}
